package com.yiwei.zxp.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.yiwei.zxp.R;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.AddressLocationBean;
import com.yiwei.zxp.api.bean.UploadImgBean;
import com.yiwei.zxp.base.BaseActivity;
import com.yiwei.zxp.util.ImageUtil;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.UI;
import com.yiwei.zxp.view.SelectAreaDilaog;
import com.yiwei.zxp.view.SureAddressDilaog;
import com.yiwei.zxp.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class MyAddressSetActivity extends BaseActivity implements SelectAreaDilaog.OnChooseListener, SureAddressDilaog.OnChooseListener {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.txt_address)
    TextView txtAddress;
    @BindView(R.id.img_nickname_right)
    ImageView imgNicknameRight;
    @BindView(R.id.btn_submit)
    SuperButton btnSubmit;
    private SelectAreaDilaog selectAreaDialog;
    private Unbinder unbiner;
    private AddressLocationBean submitData;
    private SureAddressDilaog sureAddressDilaog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_location_set);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("常用居住地");
        sureAddressDilaog = new SureAddressDilaog(this).setOnChooseListener(this);
        selectAreaDialog = new SelectAreaDilaog(this).setOnChooseListener(this);
        submitData = new AddressLocationBean();

    }

    public void getUserAddress() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();

        Api.api_service.getUserAddress(submitData).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "保存成功");
                    finish();
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }


    @OnClick({R.id.btn_back, R.id.txt_address, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.txt_address:
                selectAreaDialog.show();
                break;
            case R.id.btn_submit:
                if (TextUtils.isEmpty(UI.toString(txtAddress))) {
                    ToastUtil.makeText(this, "请选择省/市/区或县或镇");
                    return;
                }
                sureAddressDilaog.setInviteData(UI.toString(txtAddress));
                sureAddressDilaog.show();

                break;
        }
    }

    @Override
    public void onAddressSelect(String provinceId, String provinceName, String cityId, String cityName, String areaId, String areaName) {


        submitData.province_id = provinceId;
        submitData.city_id = cityId;
        submitData.district_id = areaId;

        if (TextUtils.isEmpty(provinceName)) {
            provinceName = "";
        }
        if (TextUtils.isEmpty(cityName)) {
            cityName = "";
        }
        if (TextUtils.isEmpty(areaName)) {
            areaName = "";
        }
        StringBuilder addressBuilder = new StringBuilder();
        addressBuilder.append(provinceName).append(cityName).append(areaName);
        txtAddress.setText(addressBuilder.toString());
    }

    @Override
    public void onSure() {
        getUserAddress();
    }
}
